package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.tanhua.common.utils.JwtUtils;
import com.tanhua.common.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserService {

    @Autowired
    private RSAService rsaService;

    @Autowired
    private SettingsService settingsService;

    /**
     * 校验token，如果校验成功返回用户id，否则返回null
     *
     * @param token
     * @return
     */
    public Long checkToken(String token) {
        Map<String, Object> map = JwtUtils.checkToken(token, this.rsaService.getPublicKey());
        if (CollUtil.isEmpty(map)) {
            return null;
        }
        return Convert.toLong(map.get("id"));
    }

    public void updateNotification(Boolean likeNotification, Boolean pinglunNotification, Boolean gonggaoNotification) {
        Long userId = UserThreadLocal.get();
        this.settingsService.updateNotification(userId, likeNotification, pinglunNotification, gonggaoNotification);
    }
}
